//通过vue-router插件实现模板路由配置
import { createRouter, createWebHashHistory, useRouter } from 'vue-router'
import { constantRoute } from './routes'
import useUserStore from '@/store/modules/user'

//创建路由器
let $router = useRouter()
const router = createRouter({
  //路由模式hash
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes: [...constantRoute],
  //滚动行为
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    }
  },
})

router.beforeEach(async (to: any, from: any, next: any) => {
  const tokend = localStorage.getItem('TOKEN')
  const routersa = JSON.parse(localStorage.getItem('ROUTERS'))

  //用户登录判断
  if (to.path == '/login' || to.path == '/') {
    next()
  } else {
    if (tokend == null || tokend == '') {
      next('/')
    }
    let userStore = useUserStore()
     userStore.getroutess(routersa)
    next()
  }
})

export default router
